package com.industry.examinaion.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.industry.examinaion.dal.dataobject.ExaminaionBank;
import com.industry.examinaion.dal.dto.ExamCreateRuleDto;
import com.industry.examinaion.service.ExaminaionBankService;
import com.industry.framework.common.Result;
import com.industry.framework.core.annountions.monitor.IdooMonitorService;
import com.industry.framework.core.generic.AbstractCoreGenericController;
import com.industry.system.dal.dataobject.SystemUser;
import com.industry.system.dal.dataobject.SystemUserRole;
import com.industry.system.dal.db.SystemUserMapper;
import com.industry.system.dal.db.SystemUserRoleMapper;
import com.industry.system.service.SystemRoleService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("examinaion-bank")
public class ExaminaionBankController extends AbstractCoreGenericController<ExaminaionBank, String> {
    @Resource
    ExaminaionBankService examinaionBankService;

    @Resource
    SystemUserRoleMapper systemUserRoleMapper;

    @Resource
    SystemUserMapper systemUserMapper;


    @IdooMonitorService("生成试卷")
    @PostMapping("/create-paper")
    public Result<Boolean> createPaper(@RequestBody List<ExamCreateRuleDto> createRuleDtos) {
        try {
            //查询某个角色下的所有id
            List<SystemUserRole> systemUserRoles = systemUserRoleMapper.selectList(Wrappers.lambdaQuery(SystemUserRole.class).eq(SystemUserRole::getRoleId, "1939661861148364802"));
            List<String> userIds = systemUserRoles.stream().map(SystemUserRole::getUserId).collect(Collectors.toList());
            if(CollectionUtils.isEmpty(userIds)){
                return Result.fail("未查找到当前角色下的用户信息");
            }
            List<SystemUser> systemUsers = systemUserMapper.selectByIds(userIds);
            //查询user信息
            for (SystemUser user : systemUsers) {
                examinaionBankService.createExaminationPaper(createRuleDtos,user.getId(),user.getNickName());
            }
            return Result.success(true);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

}
